package com.lzw.controller.dormitoryAdmin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.LeaveRecord;
import com.lzw.service.dormitoryAdmin.LeaveRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/leave")
@CrossOrigin(origins = "http://localhost:7070")
public class LeaveRecordController {

   @Autowired
   private LeaveRecordService leaveRecordService;

   //分页查询
   @GetMapping("/list")
   public PageInfo<LeaveRecord> list(
           @RequestParam(defaultValue = "1") Integer pageNum,
           @RequestParam(defaultValue = "10") Integer pageSize,
           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate){
      PageHelper.startPage(pageNum,pageSize);
      List<LeaveRecord> list = leaveRecordService.list(pageNum,pageSize,startDate,endDate);
      return new PageInfo<>(list);
   }

   //编辑审核请假记录
   @Log
   @PutMapping("/update")
   public String update(@RequestBody LeaveRecord leaveRecord){
      leaveRecordService.update(leaveRecord);
      return "审核请假记录成功";
   }

   //批量删除请假记录
   @Log
   @DeleteMapping("/delete/{ids}")
   public String delete(@PathVariable List<Integer> ids){
      leaveRecordService.delete(ids);
      return "删除成功";
   }

   //根据Id查询请假记录
   @GetMapping("/get/{id}")
   public LeaveRecord getLeaveRecordById(@PathVariable Integer id){
      return leaveRecordService.getById(id);
   }
}
